package com.jerry.huihome.module.find_near_mine.other;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.widget.ImageView;

import com.jerry.huihome.R;

/**
 * Created by axiang on 2016/5/12.
 */
public class RoundImageview extends ImageView{
    public static final int TYPE_CIRCLE=0;
    public static final int TYPE_ROUND=1;
    private int type;
    private int mRadius;

    public RoundImageview(Context context){
        this(context,null);
    }

    public RoundImageview(Context context, AttributeSet attrs){
        super(context, attrs);
        initviews(context,attrs);
    }

    private void initviews(Context context, AttributeSet attrs){
        Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        //获取自定义属性
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.RoundImageView);
        //获取类型
        //RoundImageView_type而不是RoundImageView
        type = array.getInt(R.styleable.RoundImageView_type, TYPE_CIRCLE);
        //默认半径
        int pxv = dp2px(50);
        mRadius = array.getDimensionPixelSize(R.styleable.RoundImageView_radius, pxv);
        array.recycle();
    }

    private int dp2px(int pxv){
        DisplayMetrics metrics = getResources().getDisplayMetrics();
        //获取dip单位
        int dipUnit = TypedValue.COMPLEX_UNIT_DIP;
        //转换
        TypedValue.applyDimension(dipUnit,pxv,metrics);
        return (int)pxv;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        if(type==TYPE_CIRCLE){
            Math.min(getMeasuredWidth(),getMeasuredHeight());
        }
    }

    @Override
    protected void onDraw(Canvas canvas){
        //??????????????????????????????为什么需要注释掉
        //父类中有mDrawable.draw(canvas);而我们已经draw(),所以注释掉
        //super.onDraw(canvas);


    }
}
